package ferp.android.ads;

import android.content.Context;
import android.os.Bundle;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.mediation.MediationAdRequest;
import com.google.android.gms.ads.mediation.customevent.CustomEventBanner;
import com.google.android.gms.ads.mediation.customevent.CustomEventBannerListener;
import com.inneractive.api.ads.sdk.InneractiveAdView;
import ferp.core.log.Log;

/**
 * User: igorgok
 * Date: 10/11/12 4:54 PM
 */
public class InneractiveCustomEventBanner implements CustomEventBanner
{
  @Override
  public void requestBannerAd(final Context context, final CustomEventBannerListener listener, String s, AdSize adSize, MediationAdRequest mediationAdRequest, Bundle bundle)
  {
    banner = new InneractiveAdView(context, "Personal_Ferp_Android", InneractiveAdView.AdType.Banner);
    banner.setMediationName(InneractiveAdView.MediationName.ADMOB);

    Log.debug(Log.TAG, "INNERACTIVE custom event #" + this.hashCode() + " received");

    banner.setBannerAdListener(new InneractiveAdView.InneractiveBannerAdListener()
    {
      @Override
      public void inneractiveBannerLoaded(InneractiveAdView iav)
      {
        Log.debug(Log.TAG, "inneractive AD received");
        listener.onAdLoaded(iav);
      }

      @Override
      public void inneractiveDefaultBannerLoaded(InneractiveAdView iav)
      {
        Log.debug(Log.TAG, "inneractive default AD received");
        listener.onAdLoaded(iav);
      }

      @Override
      public void inneractiveBannerFailed(InneractiveAdView iav, final InneractiveAdView.InneractiveErrorCode iec)
      {
        Log.debug(Log.TAG, "inneractive AD failed, error: " + iec);

        listener.onAdFailedToLoad(iec.ordinal());
      }

      @Override
      public void inneractiveBannerClicked(InneractiveAdView iav)
      {
        //Log.debug(Log.TAG, "inneractive banner clicked!");
        listener.onAdClicked();
      }

      @Override
      public void inneractiveInternalBrowserDismissed(InneractiveAdView iav)
      {
        //Log.debug(Log.TAG, "inneractive screen dismissed");
        listener.onAdClosed();
      }

      @Override
      public void inneractiveAdWillOpenExternalApp(InneractiveAdView iav)
      {
        listener.onAdLeftApplication();
      }

      @Override
      public void inneractiveBannerExpanded(InneractiveAdView iav) {}
      @Override
      public void inneractiveBannerCollapsed(InneractiveAdView iav) {}
      @Override
      public void inneractiveBannerResized(InneractiveAdView iav) {}
    });

    banner.loadAd();
  }

  @Override
  public void onDestroy()
  {
    Log.debug(Log.TAG, "destroying inneractive custom event #" + this.hashCode());

    if (banner != null)
    {
      banner.destroy();
      banner = null;
    }
  }

  @Override
  public void onPause() {}
  @Override
  public void onResume() {}

  private InneractiveAdView banner;

  static
  {
    InneractiveAdView.Timeout.setConnectionTimeout(10000);
    InneractiveAdView.Timeout.setSocketTimeout(10000);
    InneractiveAdView.Timeout.setLoadingTimeout(20000);
  }
}
